package com.central.game.controller;

import com.central.game.model.ResourceInfo;
import com.central.game.model.VersionInfo;
import com.central.game.service.VersionService;
import com.central.model.common.PageResult;
import com.central.model.common.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
public class VersionController {

    @Autowired
    private VersionService versionService;

    @PreAuthorize("hasAuthority('game:get/version')")
    @ApiOperation(value = "版本列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/version")
    public PageResult<VersionInfo> getVersion(@RequestParam Map<String, Object> params) {
        Date date = new Date();
        PageResult<VersionInfo> result = versionService.findVersions(params);
        log.info("版本查询耗时:" + (new Date().getTime() - date.getTime()));
        return result;
    }

    @PreAuthorize("hasAuthority('game:get/version/saveOrUpdate')")
    @ApiOperation(value = "版本修改或添加")
    @PostMapping("/version/saveOrUpdate")
    public Result versionSaveOrUpdate(@RequestBody VersionInfo versionInfo) {
        try {
            String success;
            if (versionInfo.getId() != null) {
                versionService.versionUpdate(versionInfo);
                success = "修改成功";
            } else {
                success = "添加成功";
                versionService.versionSave(versionInfo);
            }
            return Result.succeed(success);
        } catch (Exception ex) {
            return Result.failed("操作失败");
        }
    }

    @PreAuthorize("hasAuthority('game:delect/version/delVersion')")
    @ApiOperation(value = "版本删除")
    @DeleteMapping("/version/del/{id}")
    public Result versionDel(@PathVariable Long id) {
        try {
            String success;
            versionService.versionDel(id);
            success = "删除成功";
            return Result.succeed(success);
        } catch (Exception ex) {
            return Result.failed("删除失败");
        }
    }
}
